package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.anhuanjia.tools.eao.bean.EaoSensitivePoints;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.i18n.Lang;
import com.efastsoft.framework.ui.control.CommonDataGridAction;
import com.efastsoft.framework.utility.StringUtil;

/**
 * 周边敏感点情况表操作
 * 
 * @author Administrator
 *
 */

@Alias(value = "sensitivePoints")
public class SensitivePointsUI extends UIFacade {

	/**
	 * 添加敏感点情况
	 */
	@Priv(loginType = LoginType.Member)
	public void addOrUpdate() {
		String sensitivePointsList = $V("SensitivePointsList");
		if (StringUtil.isNotEmpty(sensitivePointsList)) {
			Transaction transaction = new Transaction();
			List<Map<String, Object>> array = JSON.parseObject(sensitivePointsList,
					new TypeReference<List<Map<String, Object>>>() {
					});
			List<String> spids = new ArrayList<>();
			String project_id = null;
			for (Map<String, Object> list : array) {
				System.out.println(list);
				EaoSensitivePoints sensitivePoints = new EaoSensitivePoints();
				sensitivePoints.setValue(list);
				project_id = sensitivePoints.getProjectId();
				String id = sensitivePoints.getSpId();
				if (StringUtils.isEmpty(id)) {
					// 添加
					sensitivePoints.setSpId(UUID.randomUUID().toString());
					transaction.insert(sensitivePoints);
					spids.add(sensitivePoints.getSpId());
				} else {
					// 更新
					spids.add(id);
					DataTable dt = new Q(" select * from eao_sensitive_points where SpId = ?", id).fetch();
					if (dt.getRowCount() != 0) {
						EaoSensitivePoints entity = new EaoSensitivePoints();
						entity.setValue(dt.get(0));
						entity.setValue(list);
						transaction.update(entity);
					}
				}
			}
			DataTable dt = new Q(" select * from eao_sensitive_points where ProjectId = ?", project_id).fetch();
			if (dt.getRowCount() != 0) {
				for (DataRow dataRow : dt) {
					EaoSensitivePoints sensitivePoints = new EaoSensitivePoints();
					sensitivePoints.setValue(dataRow);
					Boolean del = true;
					for (String spid : spids) {
						if (sensitivePoints.getSpId().equals(spid)) {
							del = false;
						}
					}
					if (del) {
						transaction.delete(sensitivePoints);
					}
				}
			}
			if (transaction.commit()) {
				s("操作成功");
			} else {
				f("操作失败");
			}
		} else {
			this.Response.put("data", "无操作记录");
		}

	}

	/**
	 * 根据项目ID删除记录
	 */
	@Priv(loginType = LoginType.Member)
	public void delete() {
		String projectId = $V("Id");
		if (!StringUtil.isNotEmpty(projectId)) {
			f("操作失败，请确认操作信息");
			return;
		}
		Transaction trans = new Transaction();
		Q q2 = new Q(" delete from eao_sensitive_points  where ").in("ProjectId", projectId);
		trans.add(q2);
		if (trans.commit()) {
			s(Lang.get("Common.ExecuteSuccess"));
		} else {
			f(Lang.get("Common.ExecuteFailed"));
		}
	}

	/**
	 * 根据Id删除一条记录
	 */
	@Priv(loginType = LoginType.Member)
	public void deletebyid() {
		String id = $V("sp_id");
		if (StringUtils.isEmpty(id)) {
			f("操作失败，请确认操作信息");
			return;
		}
		Transaction trans = new Transaction();
		Q q2 = new Q(" delete from eao_sensitive_points  where ").in("SpId", id);
		trans.add(q2);
		if (trans.commit()) {
			s(Lang.get("Common.ExecuteSuccess"));
		} else {
			f(Lang.get("Common.ExecuteFailed"));
		}
	}

	/**
	 * 根据项目id查询该项目下所有周边敏感点情况
	 */
	@Priv(loginType = LoginType.Member)
	public void findByProjectId() {
		String id = $V("Id");
		if (!StringUtil.isNotEmpty(id)) {
			f("查询失败，请确认查询信息");
			return;
		}
		Q q = new Q(
				"select SpId,ProjectId ,SensitiveName, Distance , Households , EnvFunLevel from eao_sensitive_points where 1 = 1");
		q.and().eq("ProjectId", id);
		CommonDataGridAction dga = new CommonDataGridAction();
		dga.setTotal(q);
		DataTable dt = q.fetch(dga);
		dga.bindData(dt);
		List<EaoSensitivePoints> list = new ArrayList<>();
		for (DataRow dataRow : dt) {
			EaoSensitivePoints entity = new EaoSensitivePoints();
			entity.setValue(dataRow);
			entity.setSpId(dataRow.get(0).toString());
			entity.setProjectId(dataRow.get(1).toString());
			entity.setSensitiveName(dataRow.get(2).toString());
			entity.setDistance(dataRow.get(3).toString());
			entity.setHouseholds(dataRow.get(4).toString());
			entity.setEnvFunLevel(dataRow.get(5).toString());
			list.add(entity);
			this.Response.put("sensitivePoints", list);
		}
	}

	/**
	 * 查询所有
	 */
	@Priv(loginType = LoginType.Member)
	public void findAll() {
		List<EaoSensitivePoints> obj = new ArrayList<>();
		DataTable dt = new Q(
				"select SpId,ProjectId ,SensitiveName, Distance , Households , EnvFunLevel from eao_sensitive_points where 1 = 1")
						.fetch();
		if (dt.getRowCount() != 0) {
			for (DataRow dataRow : dt) {
				EaoSensitivePoints entity = new EaoSensitivePoints();
				entity.setValue(dataRow);
				entity.setSpId(dataRow.get(0).toString());
				entity.setProjectId(dataRow.get(1).toString());
				entity.setSensitiveName(dataRow.get(2).toString());
				entity.setDistance(dataRow.get(3).toString());
				entity.setHouseholds(dataRow.get(4).toString());
				entity.setEnvFunLevel(dataRow.get(5).toString());
				obj.add(entity);
				this.Response.put("sensitivePoints", obj);
			}
		}
	}

}
